package com.jiezhuo.controller.jzg;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.jiezhuo.config.Config;
import com.jiezhuo.easyExcel.entity.HeardExcel;
import com.jiezhuo.easyExcel.entity.TeacherExcel;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.jzg.JzgVO;
import com.jiezhuo.service.jzg.JzgNewMenuService;
import com.jiezhuo.service.jzg.TJzgService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(tags = "教职工Excel")
@RequestMapping("/jzgExcel")
public class JzgExcelController {
    @Autowired
    Config config;

    @Autowired
    TJzgService tJzgService;

    @Autowired
    JzgNewMenuService jzgNewMenuService;


    @ApiOperation("获取在岗教师")
    @GetMapping("getOnGuardFaculty")
    public R  exportJzgInfo(JzgVO jzgVO) throws IOException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);

        if(ObjectUtils.isEmpty(jzgVO.getGzXqdm())){
            jzgVO.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jzgVO.getGzXqdm() + "'");
            if (!contains){
                jzgVO.setGzXqdm("'not'");
            }
        }
        String projectPath = System.getProperty("user.dir");
        List<TeacherExcel> list=tJzgService.getTeacherExcel(jzgVO);
        HeardExcel heardExcel = new HeardExcel();
        heardExcel.setData("注意：导出数据放在第二张开始的Sheet");
        ArrayList<HeardExcel> easyExcels = new ArrayList<>();
        easyExcels.add(heardExcel);
        String fileName = projectPath +  File.separator+"崇明区教职工基本信息清单表" + System.currentTimeMillis() + ".xlsx";
        ExcelWriter excelWriter = EasyExcel.write(fileName, HeardExcel.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "数据列表" + 1).build();
        WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "数据列表" + 2).head(TeacherExcel.class).build();
        excelWriter.write(easyExcels, writeSheet);
        excelWriter.write(list,writeSheet2);

        excelWriter.finish();
        return R.ok().data("usl",fileName);
    }



}
